package com.gun.management.module.raffle.dal.mysql.lotteryruleref;

import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.mybatis.core.mapper.BaseMapperX;
import com.gun.management.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.gun.management.module.raffle.controller.admin.lotteryruleref.vo.LotteryRuleRefPageReqVO;
import com.gun.management.module.raffle.controller.app.vo.AppLotteryRuleRespVO;
import com.gun.management.module.raffle.dal.dataobject.lotteryruleref.LotteryRuleRefDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 抽奖-规则信息 Mapper
 *
 * @author 测试账号
 */
@Mapper
public interface LotteryRuleRefMapper extends BaseMapperX<LotteryRuleRefDO> {

    default PageResult<LotteryRuleRefDO> selectPage(LotteryRuleRefPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<LotteryRuleRefDO>()
                .eqIfPresent(LotteryRuleRefDO::getLotteryId, reqVO.getLotteryId())
                .eqIfPresent(LotteryRuleRefDO::getRuleId, reqVO.getRuleId())
                .orderByDesc(LotteryRuleRefDO::getId));
    }

    @Insert("INSERT INTO raffle_lottery_rule_ref(`rule_id`, `lottery_id` ) VALUES " +
            "(#{entity.ruleId}, #{entity.lotteryId});")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertAndGetId(@Param("entity") LotteryRuleRefDO  entity);




    @Select({"<script>select B.id prize_id,B.avatar,B.name from raffle_lottery_rule_ref A " +
            "    inner join raffle_lottery_rule B ON A.rule_id = B.id and B.tenant_id = A.tenant_id " +
            "    inner join raffle_lottery C ON C.id = A.lottery_id and C.tenant_id = A.tenant_id " +
            "<where>" +
                "<if test='lotteryId != null'>" +
                " and A.lottery_id  = #{lotteryId} " +
                "</if>" +
            "   and  C.`status` = 0 " +
            "</where>" +
            " order by B.id asc " +
            "</script>"}
    )
    public List<AppLotteryRuleRespVO>  selectAppLotteryPrizeList(@Param("lotteryId")  Long lotteryId);




}